/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/
#ifndef OAUTHMANAGER_H
#define OAUTHMANAGER_H

#include <QtCore>
#include <QtNetwork>

#include "controls/ftmessagebox.h"

typedef QMap<QByteArray,QByteArray> OauthParams;
class QNetworkReply;

class OauthManager : public QObject
{
    Q_OBJECT
public:
    enum OauthStatus { NOAUTH, WAIT_PIN, AUTH };

    explicit OauthManager(QObject *parent, QByteArray t, QByteArray ts);
    ~OauthManager();

    bool getRequestToken();

    bool getAccessToken(const QByteArray& pin);

    QByteArray getData(const QString url,
                       const OauthParams& params,
                       QString title);

    QByteArray postData(const QString url,
                        const OauthParams& params,
                        QString title);

    bool getToken(QByteArray& retToken, QByteArray& retTokenSecret);

    QString getAuthUrl();

    OauthManager::OauthStatus getStatus();
    bool getError();

    OauthParams processRawReply();

signals:
    void requestComplete( OauthParams& );

public slots:
    void parseReply( QNetworkReply *reply );

private:
    OauthManager::OauthStatus status;
    QString baseUrl;

    QByteArray consumerSecret;
    QByteArray consumerKey;

    QByteArray token;
    QByteArray tokenSecret;

    QByteArray rawReply;

    bool error;

    void openWait( QString title, QNetworkAccessManager*, QNetworkReply*,qint64 );

    QByteArray sign(const QString &url,
                       const QByteArray &token,
                       const QByteArray &tokenSecret,
                       OauthParams& params,
                       QString mode);

    QByteArray performRequest( const QString &url,
                                const QByteArray &token ,
                                const QByteArray &tokenSecret ,
                                const OauthParams &params,
                                QString mode,
                                QString title );

    QByteArray convertParams(const OauthParams& params,
                                           QString middle,
                                           QString end);

};

#endif // OAUTHMANAGER_H
